<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .body,
        html {
            background-color: antiquewhite;
        }

        .box {
            margin: 200px auto;
            width: 942px;
            height: 530px;
            position: relative;
        }

        .item {
            position: absolute;
            top: 0;
            left: 0;
            /* display: none; */
            opacity: 0;
            transition: all 1.5s;
        }

        .item.on {
            display: block;
            opacity: 1;
        }
        .lb{
            list-style: none;
            position: absolute;
            bottom: 15px;
            left: 50%;
            transform: translate(-64%,0);
        }
        .lb-item{
            margin-left: 15px;
            float: left;
            width: 13px;
            height: 13px;
            border-radius: 50%;
            background-color: white;
        }
        .lb-item.active{
            background-color:mediumaquamarine
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="item on">
            <img src="./img/1.webp" alt="">
        </div>
        <div class="item">
            <img src="./img/2.webp" alt="">
        </div>
        <div class="item">
            <img src="./img/3.webp" alt="">
        </div>
        <div class="item">
            <img src="./img/4.webp" alt="">
        </div>
        <ul class=lb>
            <li class="lb-item active"></li>
            <li class="lb-item"></li>
            <li class="lb-item"></li>
            <li class="lb-item"></li>
        </ul>
    </div>

    <script>
        let item = document.querySelectorAll(".item")
        let index = 0
        let yd =document.querySelectorAll(".lb-item")
        let timeID = null
        //自动播放
        function auto() {
            timeID = setInterval(function () {
                item[index].classList.remove("on")
                yd[index].classList.remove("active")
                index++
                if (index === 4) {
                    index = 0
                }
                item[index].classList.add("on")
                yd[index].classList.add("active")
            }, 1500)
        }
        auto()
        //移入移出
        let box = document.querySelector(".box")
        box.addEventListener("mouseenter", function () {
            clearTimeout(timeID)
            timeID = null
        })
        box.addEventListener("mouseleave", function () {
            auto()
        })


        //圆点指示器
        yd.forEach(function(y,i){
            y.addEventListener("click",function(){
                yd[index].classList.remove("active")
                item[index].classList.remove("on")

                y.classList.add("active")
                item[i].classList.add("on")
                index = i
            })
        })
    </script>
</body>

</html>